
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERVER_IP "121.40.102.47"
#define PORT 3306

typedef struct {
    int type;
    int res;
    char account[50];   // 用户账号
    char password[50];  // 用户密码
} Login;

typedef struct {
    int id;
    char name[50];                // 领取人的姓名
    char contact_way[50];         // 联系方式
    char Instrument_number[50];   // 仪器编号
    char bor_time[50];            // 领取的时间
    char ret_time[50];            // 归还的时间
    char state[20];               // 设备状态（是否正常）
} Instrument;

typedef struct {
    int type;
    int res;
    // 环境参数
    float temp;       // 温度
    float humidity;   // 湿度
    float O2;         // 氧气浓度
    float CO2;        // 二氧化碳浓度
    float CH4;        // 甲烷浓度
    float CO;         // 一氧化碳浓度

    // 时间与位置信息
    char time[50];    // 时间
    char pt[50];      // 点位名称

    // 责任人信息
    char principal[50];// 负责人姓名
    char state[50];    // 设备状态（是否正常）
} Info;

typedef struct {
    char name[50];
    char gender[50];
    char age[50];
    char telephone[50];
    char account[50];
    char password[50];
} Message;

// 处理客户端连接逻辑的函数
void handleClientConnection() {
    int client_socket;
    struct sockaddr_in server_addr;

    // 创建客户端套接字
    client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket == -1) {
        perror("Error creating client socket");
        return;
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
        perror("Invalid address/ Address not supported");
        close(client_socket);
        return;
    }

    // 连接到服务器
    if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("Connection failed");
        close(client_socket);
        return;
    }

    char operation[10];
    char table_name[50];
    char data_str[1024];
    char response[2048];
    char request[2048];

    while (1) {
        printf("Enter operation (insert/query/exit): ");
        scanf("%s", operation);

        if (strcmp(operation, "insert") == 0) {
            printf("Enter table name (login/info/Instrument/Safety_officer/manager): ");
            scanf("%s", table_name);
            if (strcmp(table_name, "login") == 0) {
                Login pro;
                printf("Enter type: ");
                scanf("%d", &pro.type);
                printf("Enter res: ");
                scanf("%d", &pro.res);
                printf("Enter account: ");
                scanf("%s", pro.account);
                printf("Enter password: ");
                scanf("%s", pro.password);

                snprintf(request, sizeof(request), "insert:%s:%d %d %s %s", table_name, pro.type, pro.res, pro.account, pro.password);
                ssize_t bytes_sent = write(client_socket, request, strlen(request));
                if (bytes_sent <= 0) {
                    perror("Error sending request to server");
                    close(client_socket);
                    return;
                }
                ssize_t bytes_read = read(client_socket, response, sizeof(response) - 1);
                if (bytes_read <= 0) {
                    perror("Error reading response from server");
                    close(client_socket);
                    return;
                }
                response[bytes_read] = '\0';
                printf("Response from server: %s\n", response);
            } else if (strcmp(table_name, "info") == 0) {
                Info infor;
                printf("Enter type: ");
                scanf("%d", &infor.type);
                printf("Enter res: ");
                scanf("%d", &infor.res);
                printf("Enter temp: ");
                scanf("%f", &infor.temp);
                printf("Enter humidity: ");
                scanf("%f", &infor.humidity);
                printf("Enter O2: ");
                scanf("%f", &infor.O2);
                printf("Enter CO2: ");
                scanf("%f", &infor.CO2);
                printf("Enter CH4: ");
                scanf("%f", &infor.CH4);
                printf("Enter CO: ");
                scanf("%f", &infor.CO);
                printf("Enter time: ");
                scanf("%s", infor.time);
                printf("Enter pt: ");
                scanf("%s", infor.pt);
                printf("Enter principal: ");
                scanf("%s", infor.principal);
                printf("Enter state: ");
                scanf("%s", infor.state);

                snprintf(request, sizeof(request), "insert:%s:%d %d %f %f %f %f %f %f %s %s %s %s", table_name, infor.type, infor.res, infor.temp, infor.humidity, infor.O2, infor.CO2, infor.CH4, infor.CO, infor.time, infor.pt, infor.principal, infor.state);
                ssize_t bytes_sent = write(client_socket, request, strlen(request));
                if (bytes_sent <= 0) {
                    perror("Error sending request to server");
                    close(client_socket);
                    return;
                }
                ssize_t bytes_read = read(client_socket, response, sizeof(response) - 1);
                if (bytes_read <= 0) {
                    perror("Error reading response from server");
                    close(client_socket);
                    return;
                }
                response[bytes_read] = '\0';
                printf("Response from server: %s\n", response);
            } else if (strcmp(table_name, "Instrument") == 0) {
                Instrument data;
                printf("Enter id: ");
                scanf("%d", &data.id);
                printf("Enter name: ");
                scanf("%s", data.name);
                printf("Enter contact_way: ");
                scanf("%s", data.contact_way);
                printf("Enter Instrument_number: ");
                scanf("%s", data.Instrument_number);
                printf("Enter bor_time: ");
                scanf("%s", data.bor_time);
                printf("Enter ret_time: ");
                scanf("%s", data.ret_time);
                printf("Enter state: ");
                scanf("%s", data.state);

                snprintf(request, sizeof(request), "insert:%s:%d %s %s %s %s %s %s", table_name, data.id, data.name, data.contact_way, data.Instrument_number, data.bor_time, data.ret_time, data.state);
                ssize_t bytes_sent = write(client_socket, request, strlen(request));
                if (bytes_sent <= 0) {
                    perror("Error sending request to server");
                    close(client_socket);
                    return;
                }
                ssize_t bytes_read = read(client_socket, response, sizeof(response) - 1);
                if (bytes_read <= 0) {
                    perror("Error reading response from server");
                    close(client_socket);
                    return;
                }
                response[bytes_read] = '\0';
                printf("Response from server: %s\n", response);
            } else if (strcmp(table_name, "Safety_officer") == 0 || strcmp(table_name, "manager") == 0) {
                Message message;
                printf("Enter name: ");
                scanf("%s", message.name);
                printf("Enter gender: ");
                scanf("%s", message.gender);
                printf("Enter age: ");
                scanf("%s", message.age);
                printf("Enter telephone: ");
                scanf("%s", message.telephone);
                printf("Enter account: ");
                scanf("%s", message.account);
                printf("Enter password: ");
                scanf("%s", message.password);

                snprintf(request, sizeof(request), "insert:%s:%s %s %s %s %s %s", table_name, message.name, message.gender, message.age, message.telephone, message.account, message.password);
                ssize_t bytes_sent = write(client_socket, request, strlen(request));
                if (bytes_sent <= 0) {
                    perror("Error sending request to server");
                    close(client_socket);
                    return;
                }
                ssize_t bytes_read = read(client_socket, response, sizeof(response) - 1);
                if (bytes_read <= 0) {
                    perror("Error reading response from server");
                    close(client_socket);
                    return;
                }
                response[bytes_read] = '\0';
                printf("Response from server: %s\n", response);
            }
        } else if (strcmp(operation, "query") == 0) {
            printf("Enter table name (login/info/Instrument/Safety_officer/manager): ");
            scanf("%s", table_name);
            printf("Enter query condition: ");
            char query_condition[1024];
            scanf(" %[^\n]", query_condition);

            snprintf(request, sizeof(request), "query:%s:%s", table_name, query_condition);
            ssize_t bytes_sent = write(client_socket, request, strlen(request));
            if (bytes_sent <= 0) {
                perror("Error sending request to server");
                close(client_socket);
                return;
            }
            ssize_t bytes_read = read(client_socket, response, sizeof(response) - 1);
            if (bytes_read <= 0) {
                perror("Error reading response from server");
                close(client_socket);
                return;
            }
            response[bytes_read] = '\0';
            printf("Response from server: %s\n", response);
        } else if (strcmp(operation, "exit") == 0) {
            break;
        } else {
            printf("Invalid operation.\n");
        }
    }

    close(client_socket);
}

int main() {
    handleClientConnection();
    return 0;
}
